<template>
    <div>
        <box t1="商品管理" t2="商品列表">
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query">
                        <el-button slot="append" @click="getGoodsList()" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button @click="goToAdd" type="primary">添加商品</el-button>
                </el-col>
            </el-row>
            <br>
            <!-- 表格区域 -->
            <el-table :data="goodsList" border="" stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品价格(元)" prop="goods_price" width="105px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
                <el-table-column label="创建时间" prop="add_time" width="140px">
                    <template slot-scope="scope">{{ scope.row.add_time | dateformat }}</template>
                </el-table-column>
                <el-table-column label="操作" width="130px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <el-button
                            type="danger"
                            @click="deleteGoods(scope.row.goods_id)"
                            icon="el-icon-delete"
                            size="mini"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br>
            <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[10, 15, 50, 100]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            ></el-pagination>
        </box>
    </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      queryInfo: {
        query: "",
        pagesize: 10,
        pagenum: 1
      },
      total: 0
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //定义获取商品列表数据的函数
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 分页组件中 改变每页显示多少条数据的事件执行函数 val就是组件传递过来的最新的每页显示多少条数据
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    // 分页组件中 改变页码值的事件执行函数 val就是组件传递过来的最新的页码值
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    // 删除商品的函数
    async deleteGoods(goods_id) {
      // 调用confirm弹窗
      let result = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(error => error);

      if (result != "confirm") {
        return this.$message.info("用户取消删除");
      }

      // 如果确定删除 则发送删除的请求
      const { data: res } = await this.$http.delete("goods/" + goods_id);
      if(res.meta.status!==200){
          return this.$message.error('删除失败！')
      }
      // 成功则重新加载页面
      this.getGoodsList()
    },
    // 导航到添加页面
    goToAdd(){
        this.$router.push('/goods/add')
    }
  }
};
</script>

<style lang="less" scoped>
</style>